package com.daxue.bootckafka.controller;

import com.daxue.bootckafka.send.TestMessageSender;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author daxue0929
 * @date 2022/10/24
 */

@RestController
@RequestMapping(value = "/producer")
public class SendController {

    private final KafkaTemplate kafkaTemplate;

    final TestMessageSender sender;

    public SendController(KafkaTemplate kafkaTemplate, TestMessageSender sender) {
        this.kafkaTemplate = kafkaTemplate;
        this.sender = sender;
    }

    /***
     * 发送消息
     * topic:要发送的队列
     * msg:发送的消息
     */
    @PostMapping(value = "/send/{topic}")
    public String send(
        @PathVariable(value = "topic") String topic, @RequestBody Object msg) {
        //消息发送
//        kafkaTemplate.send(topic,msg);
        sender.send(msg);
        return "SUCCESS";
    }


    @PostMapping(value = "/send_to_test")
    public String sendToTest(@RequestBody Person msg) {
        sender.send(msg);
        return "SUCCESS";
    }
}
